﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Text;
using Cosmetic.DataAccess;
using Cosmetic.DataEntity;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Cosmetic.Logic
{
    public  class CashCardManger:LogicBase
    {
        public void Insert(CashCard  aProduct)
        {
            m_CosmeticDC.CashCard .InsertOnSubmit(aProduct);
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// Delete product
        /// </summary>
        /// <param name="aProduct">The product want delete</param>
        public void Delete(CashCard  aProduct)
        {
            m_CosmeticDC.CashCard .DeleteOnSubmit(aProduct);
            m_CosmeticDC.SubmitChanges();
        }

        /// <summary>
        /// 判断是否存在次卡号
        /// </summary>
        /// <param name="anumber">The product want delete</param>

        public CashCard  CheckExistes(string uername)
        {
            List<CashCard > theProductList = (from theProduct in m_CosmeticDC.CashCard  
                                            where theProduct.CardNum == uername
                                            select theProduct).ToList();
            if (theProductList.Count == 1)
            {
                return theProductList[0];
            }
            return null;
        }

        /// <summary>
        /// 判断是否存在次卡号
        /// </summary>
        /// <param name="anumber">The product want delete</param>

        public bool CheckExists(string uername)
        {
            List<CashCard > theProductList = (from theProduct in m_CosmeticDC.CashCard 
                                            where theProduct.CardNum == uername
                                            select theProduct).ToList();
            return theProductList.Count > 0;
        }


        public static void UpdateInformation(string  id, bool a, Guid bb)
        {
            string conStr = ConfigurationManager.ConnectionStrings["CosmeticConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(conStr);
            try
            {
                con.Open();
                string cmdStr = string.Empty;

                cmdStr = "UPDATE [CashCard]  SET [IsUsed] ='" + a + "'  ,[UserId] = '" + bb + "' WHERE CardNum=" + id + "";
                SqlCommand com = new SqlCommand(cmdStr, con);
                com.ExecuteNonQuery();
            }
            finally
            {
                con.Close();
            }
        }

    }
}
